package com.dyh.quickdemo.demos.resTemplateInsp;

import java.util.Timer;
import java.util.TimerTask;

/*
@Author=Joey Wheeler
@Date=2020/10/19 17:32
*/
public class ThreadKillTask extends TimerTask {

    static Timer timer = new Timer("ThreadKillTaskTimer", true);

    protected Thread target = null;//被监视的Thread

    /*监视自己*/
    public ThreadKillTask(long millis) {
        target = Thread.currentThread();
        timer.schedule(this, millis);
    }


    public ThreadKillTask(Thread target, long millis) {
        this.target = target;
        timer.schedule(this, millis);
    }

    /*父类方法包了一层try catch*/
    public boolean cancel() {
        try {
            return super.cancel();
        } catch (Exception e) {
            return false;
        }
    }


    @Override
    public void run() {
        if ((target != null) && (target.isAlive()))
            target.interrupt();
    }
}
